/**
 * @(#)HotWordServiceImpl.java 2017年5月26日
 * Copyright(C) 2015 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IHotwordDAO;
import org.jsbd.boss.dao.IWeixinIdphotoDao;
import org.jsbd.boss.dao.IWeixinNickNameDao;
import org.jsbd.boss.service.IHotWordService;
import org.jsbd.boss.util.CacheKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * TODO 请加入本类的说明
 * @author <a href="mailto:zhangbinghua@zhexinit.com" >zbh</a>
 * @version 1.0.0
 */
@Service
public class HotWordServiceImpl implements IHotWordService {
	public static final Logger LOGGER = LoggerFactory.getLogger(HotWordServiceImpl.class);

	@Resource
	private IHotwordDAO hotWordDAO;
	@Resource
	private IWeixinIdphotoDao weixinIdphotoDao;
	@Resource
	private IWeixinNickNameDao weixinNickNameDao; 
	@Resource
	private RedisClient<String, Object> redisClient;
	
	@Override
	public List<String> selectHotwords(int length) {
		return hotWordDAO.selectHotwords(length);
	}

	@Override
	public void saveHotwords(String word) {
		hotWordDAO.saveHotwords(word);
	}

	@Override
	public String getIdPhoto() {
		String key = CacheKey.WEIXIN_IDPHOTO_KEY;
		String photo = (String) redisClient.pollFromQueue(key);
		if (StringUtils.isBlank(photo)) {
			List<String> list = weixinIdphotoDao.selectAll();
			photo = list.remove(0);
			String[] strings = new String[list.size()];
			redisClient.putToQueue(key, list.toArray(strings));
		}
		return photo;
	}

	@Override
	public String getNickName() {
		String key = CacheKey.WEIXIN_NICKNAME_KEY;
		String nickName = (String) redisClient.pollFromQueue(key);
		if (StringUtils.isBlank(nickName)) {
			List<String> list = weixinNickNameDao.selectAll();
			nickName = list.remove(0);
			String[] strings = new String[list.size()];
			redisClient.putToQueue(key, list.toArray(strings));
		}
		return nickName;
	}

}
